package com.service.account.impl;

import com.dao.account.AccountMapper;
import com.entity.Answer;
import com.entity.user.User;
import com.service.account.AccountSerice;
import com.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountSericeImpl implements AccountSerice {


    @Autowired
    AccountMapper accountMapper;
    public Object addUser(User user){
        Answer answer = new Answer();
        try {
            if (accountMapper.addUser(user)==1){
                answer.setCode(1);
            }else {
                answer.setCode(0);
                answer.setError("注册失败！");
            }
        }catch (Exception e){
            answer.setCode(0);
            answer.setError("注册失败！");
            e.printStackTrace();
            return answer;
        }
        return answer;
    }

    public Object login(User user){
        Answer answer = new Answer();
        if (user == null && user.getName() == null && user.getPwd() == null){
            answer.setCode(0);
            answer.setError("用户名或密码不可为空");
            return answer;
        }
        User u = accountMapper.login(user);
        if (u == null){
            answer.setCode(0);
            answer.setError("密码错误，或用户名不存在！");
            return answer;
        }else{
            answer.setCode(1);
            //answer.setData("{\"userID\":\""+u.getUserID()+"\"}");
            answer.setData(u);
            return answer;
        }
    }
}
